package com.tangly.codeing.service.impl;

import com.tangly.codeing.dao.SysRoleDao;
import com.tangly.codeing.model.SysRole;
import com.tangly.codeing.service.SysRoleService;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色表
 *
 * @author tangbing
 * @date 2019-05-05
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleDao sysRoleDao;

    /**
     * 新增
     */
    @Override
    public ReturnT<String> insert(SysRole sysRole) {

        // valid
        if (sysRole == null) {
            return new ReturnT<>(ReturnT.FAIL_CODE, "必要参数缺失");
        }

        sysRoleDao.insert(sysRole);
        return ReturnT.SUCCESS;
    }

    /**
     * 删除
     */
    @Override
    public ReturnT<String> delete(int id) {
        int ret = sysRoleDao.delete(id);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * 批量删除
     */
    @Override
    public ReturnT<String> batchDelete(List<String> ids) {
        int ret = sysRoleDao.batchDelete(ids);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * 更新
     */
    @Override
    public ReturnT<String> update(SysRole sysRole) {
        int ret = sysRoleDao.update(sysRole);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * Load查询
     */
    @Override
    public SysRole load(int id) {
        return sysRoleDao.load(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo pageList(PageInfo pageInfo) {

        List<SysRole> pageList = sysRoleDao.pageList(pageInfo);
        int totalCount = sysRoleDao.pageListCount(pageInfo);

        pageInfo.setResultObject(pageList);
        pageInfo.setTotalRecord(totalCount);
        return pageInfo;
    }

    /**
     * 根据用户id获取角色
     */
    @Override
    public List<SysRole> getByUserId(int userId) {
        return sysRoleDao.getByUserId(userId);
    }

    @Override
    public List<SysRole> findAll() {
        return sysRoleDao.findAll();
    }
}
